<template>
  <div class="xtx-form">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <input class="input" type="text" placeholder="请输入用户名" v-model="form.account" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input class="input" type="text" placeholder="请输入手机号"  v-model="form.mobile"/>
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input class="input" type="text" placeholder="请输入验证码" v-model="form.code"/>
        <span class="code" @click="sendCode">{{countTime===0?'发送验证码':`${countTime}秒后重试`}}</span>
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input class="input" type="password" placeholder="请输入密码" v-model="form.password"/>
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input class="input" type="password" placeholder="请确认密码" v-model="form.rePassword"/>
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" class="submit" @click='submit'>立即提交</a>
  </div>
</template>

<script>
import { reactive } from 'vue'
import { message } from '@/components/Message'
import { reqUserQQPatchCode, reqUserQQPatchLogin } from '@/api/use'
import { useCountDown } from '@/compositions'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
export default {
  name: 'CallbackPatch',
  props: {
    unionId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    const store = useStore()
    const router = useRouter()
    const form = reactive(
      {
        mobile: '',
        code: '', // 123456
        account: '',
        password: '',
        rePassword: ''
      })
    const { countTime, start } = useCountDown(60)
    const sendCode = async () => {
      // 手机号码校验
      if (!/^1[3-9]\d{9}$/.test(form.mobile)) {
        return message({ type: 'error', text: '手机格式错误' })
      }
      // 发验证码请求
      const { msg } = await reqUserQQPatchCode(form.mobile)
      //   console.log(res)
      message({ type: 'success', text: msg })
      start()
    }
    const submit = async () => {
      if (form.password !== form.rePassword) { return message({ type: 'error', text: '密码不一致' }) }
      console.log(props.unionId)
      const { result } = await reqUserQQPatchLogin({
        unionId: props.unionId,
        ...form
      })
      //   console.log(result)
      // 保存用户信息
      store.commit('user/setProfile', result)
      //   跳转到首页
      router.push('/')
      // 合并购物车
      await store.dispatch('cart/asyncMergeLocalCart')
      //   提示用户
      message({ type: 'success', text: 'QQ完善信息成功' })
    }
    return { form, sendCode, countTime, submit }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
